package jp.sinmetal.trainer.action;

import java.util.List;

import javax.annotation.Resource;

import jp.sinmetal.trainer.entity.ConventionMember;
import jp.sinmetal.trainer.exception.ConventionException;
import jp.sinmetal.trainer.form.ConventionMemberForm;
import jp.sinmetal.trainer.service.ConventionMemberService;

import org.apache.struts.action.ActionMessage;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.exception.ActionMessagesException;

/**
 * 大会参加者アクション
 * @author sin
 *
 */
public class ConventionMemberAction extends AbstractAction {


    /**
     * 大会参加者一覧
     * @author sin
     */
    public List<ConventionMember> conventionMembers;

    /**
     * 大会参加者アクションフォーム
     */
    @ActionForm
    @Resource
    protected ConventionMemberForm conventionMemberForm;

    /**
     * 大会参加者サービス
     */
    @Resource
    protected ConventionMemberService conventionMemberService;

    /**
     * 大会参加登録サービス呼び出し
     * @author sin
     *
     * @return
     */
    @Execute(input = "/convention", urlPattern = "insert/{conventionId}", redirect = true)
    public String insert() {
    	if (!memberInfo.isLogin()) {
    		return "/authentication/";
    	}

        ConventionMember entity = ConventionMember.getInstance(conventionMemberForm);

        entity.memberId = memberInfo.memberId;

        try {
        	conventionMemberService.insertConventionMember(entity);
        } catch(ConventionException e) {
        	ActionMessagesException ex = new ActionMessagesException("It's a dummy!!");
			ex.addMessage(e.getProperty(), new ActionMessage(e.getMessage(),false));
			throw ex;
        }

        return String.format("/convention/%s", conventionMemberForm.conventionId);
    }

    /**
     * アクティブ状態登録
     * @author sin
     *
     * @return
     */
    @Execute(input = "/convention", urlPattern = "registActiveFlag/{conventionMemberId}/{conventionId}/{version}/{activeFlag}", redirect = true)
    public String registActiveFlag() {
    	ConventionMember entity = ConventionMember.getInstance(conventionMemberForm);
    	conventionMemberService.update4ActiveFlag(entity);
    	return String.format("/convention/%s", conventionMemberForm.conventionId);
    }
}